<?php
namespace app\api\model;

use app\admin\event\SystemLog;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\db\Query;
use think\exception\DbException;
use think\Model;
use think\Db;

class UserModel extends Model
{

    protected static function init()
    {
        self::observe(SystemLog::class);
    }


    use CanGetFieldModel, SimplePaginate;
    protected $table = 'jxc_user';
    protected $autoWriteTimestamp = 'int';
    protected $updateTime = false;
    protected $type = ['mode_des' => 'json', 'birthday' => 'datetime'];

    public function searchNameAttr(Query $query, $value, $data)
    {
        $query->whereLike('name', "%" . $value . "%");
    }

    public function searchCategoryAttr(Query $query, $value, $data)
    {
        $query->whereCategory($value);
    }

    public function searchMemberAttr(Query $query, $value, $data)
    {
        $query->whereMember($value);
    }

    public function searchSourceAttr(Query $query, $value, $data)
    {
        $query->whereSource($value);
    }

    public function searchStatusAttr(Query $query, $value, $data)
    {
        $query->whereStatus($value);
    }

    public function searchTimeAttr(Query $query, $value, $data)
    {
        $time = array_filter(explode(',', $value));
        // 默认结束时间为当前时间
        if (!isset($time[1])) {
            $time[1] = time();
        }
        $query->whereBetweenTime('create_time', $time[0], $time[1]);
    }

    public function category()
    {
        return $this->belongsTo(CustomerCategory::class, 'category');
    }

    /**
     * @param $data
     * @param int $page
     * @return false|\PDOStatement|string|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList($data, $page = 1, $size = 10)
    {
        $data = array_filter($data);
        $list = $this->withSearch(array_keys($data), $data)->order('sort asc')->with('category')
            ->page($page, $size)->paginate($size);
//        $list = $list->toArray();
//        unset($list['per_page']);
//        unset($list['last_page']);
//        foreach ($list as $k => $v) {
//            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
////            $cate = CustomerCategory::where('id', $v['category'])->find();
////            $list[$k]['category'] = $cate['name'];
////            $list[$k]['discount'] = $cate['discount'];
//        }
        $list = $list->toArray();
//         欠款总数
        $list['total_arrears'] = $this->sum('arrears');
        return $list;
    }

    /**
     * @param $data
     * @return bool
     */
    public function getAdd($data)
    {
        return $this->save($data);
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getInfo($id)
    {
        try {
            return $this->findOrFail($id);
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param $id
     * @param $data
     * @return int|string
     */
    public function getEdit($id, $data)
    {
        try {
            return $this->findOrFail($id)->update($data, ['id' => $id]);
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param $id
     * @return int|string
     */
    public function getDel($id)
    {
        try {
            return $this->findOrFail($id)->delete();
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    public function getAllCustomer($id)
    {
        try {
            return $this->where('category',$id)->field('id,name,phone')->select();
        } catch (PDOException $e) {
            return $e->getMessage();
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}
